﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using Common.Tools;
using ResourceCollectionDomain.Entity;
using SpiderCore.Utility;
using SqlHelper = Common.Tools.SqlHelper;

namespace ResourceCollectionSys.Service
{
    public class BaiDuService
    {
        private static SqlHelper _sqlHelper =
            new SqlHelper(
                "Data Source=203.195.177.228;Initial Catalog=Discuz;User ID=sa;Password=HM57g9A3k4XCL;Integrated Security=False");

        public static string ErrorMessage { get; set; }
        public static int SuccessCount { get; set; }
        public static bool IsWorcking { get; set; }

        public static void PushUrlToBaiDu()
        {
            IsWorcking = true;
            ErrorMessage = string.Empty;
            while (IsWorcking)
            {
                try
                {
                    #region 开始执行


                    //
                    string sql3 = "SELECT Top 50 TopicID FROM dbo.SEPushRecord WHERE BaiDuSend=0";
                    var waitsendtb = _sqlHelper.ExecuteDataTable(sql3);
                    string sql4 = "update SEPushRecord set BaiDuSend=1 where TopicID=@topid";
                    if (waitsendtb.Rows.Count > 0)
                    {
                        string posturl = "http://data.zz.baidu.com/urls?site=www.censon.org&token=kRJPz4itgjuN3JQ0";
                        for (int i = 0; i < waitsendtb.Rows.Count; i++)
                        {
                            string data = waitsendtb.Rows[i][0].ToString();
                            string ret = "";
                            try
                            {
                                ret = HttpRequestHelper.PostRequest(
                                   "http://www.censon.org/showtopic-" + data + ".aspx",
                                   posturl,
                                   null);
                                //推送成功
                                if (ret.Contains("success"))
                                {
                                    SqlParameter[] para1 = new SqlParameter[]
                                    {
                                        new SqlParameter("@topid", data),
                                    };
                                    _sqlHelper.ExecuteNonQuery(sql4, para1);
                                    SuccessCount++;
                                }
                                else
                                {
                                    throw new Exception("百度地址推送异常");
                                }
                            }
                            catch (Exception ex)
                            {
                                CustomsLog.WriteInfoLog("百度地址推送异常", "Url:" + ret);
                                throw;
                            }


                        }
                    }
                    else
                    {
                        Thread.Sleep(new TimeSpan(0, 1, 0, 0));//没有数据则休息1小时
                    }

                    #endregion
                }
                catch (Exception ex)
                {

                    ErrorMessage += ex.ToString();
                    Thread.Sleep(new TimeSpan(1, 1, 0, 0));//发生异常则休息24小时
                    //IsWorcking = false;
                }
            }
        }
        /// <summary>
        /// 同步推送记录
        /// </summary>
        /// <returns></returns>
        public static int SyscPushRecord()
        {
            try
            {
                //将没有在搜索引擎推送记录表中的主题ID放入之
                string sql1 =
                    "SELECT tid FROM dbo.dnt_topics WHERE tid NOT IN (SELECT TopicID FROM dbo.SEPushRecord)";
                var tb = _sqlHelper.ExecuteDataTable(sql1);
                string sql2 = "insert into SEPushRecord(TopicID) values(@TopicID)";
                if (tb.Rows.Count > 0)
                {
                    for (int i = 0; i < tb.Rows.Count; i++)
                    {
                        SqlParameter[] para1 = new SqlParameter[]
                            {
                                new SqlParameter("@TopicID", tb.Rows[i][0]),
                            };
                        try
                        {
                            _sqlHelper.ExecuteNonQuery(sql2, para1);
                        }
                        catch (Exception ex)
                        {
                            if (ex.ToString().Contains("重复键"))
                                continue;
                            throw;
                        }

                    }
                }
                return tb.Rows.Count;
            }
            catch (Exception ex)
            {

                CustomsLog.WriteExceptionErrorLog("同步推送记录", ex);
                return 0;
            }

        }
    }
}
